package datasources

import (
	"context"
	"errors"
	"github.com/goccy/go-json"

	"go-caipu/pkg/services/dto"
)

var (
	ErrDatasourceNotFound = errors.New("datasource not found")
)

type GetDatasourcePageCommand struct {
	dto.Pagination `search:"-"`
	Type           string `form:"type"  search:"type:exact;column:type;table:sys_plugin_datasource" comment:"数据源类型"`
	Name           string `form:"name"  search:"type:contains;column:name;table:sys_plugin_datasource" comment:"名称"`
}

type Datasource struct {
	Id             int64           `json:"id,string"` //
	Version        string          `json:"version" gorm:"type:bigint;comment:更新版本"`
	Type           string          `json:"type" gorm:"type:varchar(100);comment:数据源类型"`
	Name           string          `json:"name" gorm:"type:varchar(255);comment:名称"`
	Access         string          `json:"access" gorm:"type:varchar(255);comment:访问对象"`
	Url            string          `json:"url" gorm:"type:varchar(100);comment:配置地址"`
	IsDefault      int             `json:"isDefault" gorm:"type:int;comment:是否默认"`
	JsonData       json.RawMessage `json:"jsonData" gorm:"type:varchar(600);comment:配置json串"`
	SecureJsonData string          `json:"secureJsonData" gorm:"type:varchar(600);comment:加密配置"`
	ReadOnly       string          `json:"readOnly" gorm:"type:varchar(64);comment:是否只读"`
	dto.ControlBy
	dto.ModelTime
}

func (m *GetDatasourcePageCommand) GetNeedSearch() interface{} {
	return *m
}

type GetDatasourceCommand struct {
	Id string `uri:"name"`
}

type RemoveDatasourceCommand struct {
	Ids []int64 `json:"ids"`
}

// Service 业务接口
type Service interface {
	GetDatasourcePage(ctx context.Context, cmd GetDatasourcePageCommand) (data []Datasource, num int64, err error)
	Get(ctx context.Context, cmd GetDatasourceCommand) (Datasource, error)
	GetDataSourceByType(ctx context.Context, cmd string) (Datasource, error)
	Insert(ctx context.Context, cmd Datasource) (Datasource, error)
	Update(ctx context.Context, cmd Datasource) error
	Remove(ctx context.Context, cmd RemoveDatasourceCommand) error
	RemoveDSByPluginID(ctx context.Context, pluginID string, categorys []int64, apiIds []int64, menuIDs []int64) (err error)
}
